package com.example.zheng.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zheng.entity.Counts;
import com.example.zheng.entity.GroupBuildingVo;
import com.example.zheng.entity.Repairbill;
import com.example.zheng.entity.Repairman;
import com.example.zheng.service.IRepairbillService;
import com.example.zheng.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zheng
 * @since 2022-01-11
 */
@RestController
@RequestMapping("/repairbill")
public class RepairbillController {

    @Autowired
    private IRepairbillService iRepairbillService;

    //报修登记
    @PostMapping("/report")
    public Result report(@RequestBody Repairbill repairbill){
        QueryWrapper<Repairbill> wrapper = new QueryWrapper();
        wrapper.eq("building",repairbill.getBuilding());
        wrapper.eq("number",repairbill.getNumber());
        wrapper.eq("repairType",repairbill.getRepairType());
        wrapper.ge("reportTime",LocalDateTime.now().minusDays(3));
        Repairbill one = iRepairbillService.getOne(wrapper);
        if (one != null) {
            return Result.failure("已经报修过了，请三天后再重新报修");
        }
        repairbill.setReportTime(LocalDateTime.now());
        boolean save = iRepairbillService.save(repairbill);
        if (!save){
            return Result.failure("报修登记失败");
        }
        return Result.success("报修登记成功");
    }

    //待分配接口
    @GetMapping("/allocation")
    public Result allocation(@RequestParam(defaultValue = "1",name = "pageNum",required = false) Integer pageNum,
                             @RequestParam(defaultValue = "6",name = "pageSize",required = false) Integer pageSize,
                             Repairbill repairbill){

        System.out.println("repairbill="+repairbill);

//        System.out.println("pageNum=="+pageNum);
//        System.out.println("pageSize=="+pageSize);

        HashMap<String, Object> map = new HashMap<>();
        map.put("userNumber",repairbill.getUserNumber());
        map.put("repairType",repairbill.getRepairType());
        map.put("building",repairbill.getBuilding());
        map.put("repairState",0);

        Page<Repairbill> page = new Page<>(pageNum,pageSize);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.allEq(map,false);
        iRepairbillService.page(page, wrapper);
//        System.out.println(page.getRecords());
//        System.out.println(page.getTotal());
        return Result.success(page);
    }

    //发布订单

    @PutMapping("/publish/{id}")
    public Result publish(@PathVariable(name ="id") Integer id){

        Repairbill repairbill = new Repairbill();
        repairbill.setMainId(id);
        repairbill.setRepairState(1);
        boolean update = iRepairbillService.updateById(repairbill);

        if (!update){
            return Result.failure("发布失败");
        }
        return Result.success("发布成功");
    }

    // 可抢单信息获取接口
    @GetMapping("/strived")
    public Result strived(@RequestParam(defaultValue = "1",name = "pageNum",required = false) Integer pageNum,
                             @RequestParam(defaultValue = "6",name = "pageSize",required = false) Integer pageSize,
                             Repairbill repairbill){

        System.out.println("repairbill="+repairbill);

//        System.out.println("pageNum=="+pageNum);
//        System.out.println("pageSize=="+pageSize);

        HashMap<String, Object> map = new HashMap<>();
        map.put("userNumber",repairbill.getUserNumber());
        map.put("repairType",repairbill.getRepairType());
        map.put("building",repairbill.getBuilding());
        map.put("repairState",1);

        Page<Repairbill> page = new Page<>(pageNum,pageSize);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.allEq(map,false);
        iRepairbillService.page(page, wrapper);
//        System.out.println(page.getRecords());
//        System.out.println(page.getTotal());
        return Result.success(page);
    }

    //处理抢单操作
    @PutMapping("/updateStrived/{id}/{repId}")
    private Result updateStrived(@PathVariable(name ="id") Integer id,
                                 HttpServletRequest request,
                                 @PathVariable(name = "repId")Integer repId){

        Repairbill repairbill = new Repairbill();
        repairbill.setMainId(id);
        repairbill.setRepairState(2);

        //维修员id
        repairbill.setRepID(repId);

        boolean update = iRepairbillService.updateById(repairbill);
        if (!update){
            return Result.failure("抢单失败");
        }

        return Result.success("抢单成功");
    }

    // 维修中信息获取接口
    @GetMapping("/repairing")
    public Result repairing(@RequestParam(defaultValue = "1",name = "pageNum",required = false) Integer pageNum,
                          @RequestParam(defaultValue = "6",name = "pageSize",required = false) Integer pageSize,
                          Repairbill repairbill){


        HashMap<String, Object> map = new HashMap<>();
        map.put("userNumber",repairbill.getUserNumber());
        map.put("repairType",repairbill.getRepairType());
        map.put("building",repairbill.getBuilding());
        map.put("repID",repairbill.getRepID());
        map.put("repairState",2);

        Page<Repairbill> page = new Page<>(pageNum,pageSize);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.allEq(map,false);
        iRepairbillService.page(page, wrapper);
        return Result.success(page);
    }

    //完成后的信息填写
    @PutMapping("/updaterepairing")
    private Result updaterepairing(@RequestBody Repairbill repairbill){
//        System.out.println("updaterepairing%%%%%%%%==="+repairbill);
        repairbill.setRepairState(3);
        repairbill.setRepairTime(LocalDateTime.now());
        boolean update = iRepairbillService.updateById(repairbill);

        if (!update){
            return Result.failure("维修信息更新失败");
        }

        return Result.success("维修信息更新成功");
    }

    //异常单信息填写
    @PutMapping("/handleError")
    private Result handleError(@RequestBody Repairbill repairbill){
        repairbill.setRepairState(4);
        repairbill.setRepairTime(LocalDateTime.now());
        boolean update = iRepairbillService.updateById(repairbill);
        if (!update){
            return Result.failure("异常信息添加失败");
        }
        return Result.success("异常信息添加成功");
    }

    // 异常单信息获取接口
    @GetMapping("/getErrorBill")
    public Result getErrorBill(@RequestParam(defaultValue = "1",name = "pageNum",required = false) Integer pageNum,
                            @RequestParam(defaultValue = "6",name = "pageSize",required = false) Integer pageSize,
                            Repairbill repairbill){


        HashMap<String, Object> map = new HashMap<>();
        map.put("userNumber",repairbill.getUserNumber());
        map.put("repairType",repairbill.getRepairType());
        map.put("building",repairbill.getBuilding());
        map.put("repID",repairbill.getRepID());
        map.put("repairState",4);

        Page<Repairbill> page = new Page<>(pageNum,pageSize);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.allEq(map,false);
        iRepairbillService.page(page, wrapper);
        return Result.success(page);
    }

    @GetMapping("/getMyReport")
    public Result getMyReport(@RequestParam(defaultValue = "1",name = "pageNum",required = false) Integer pageNum,
                               @RequestParam(defaultValue = "6",name = "pageSize",required = false) Integer pageSize,
                               Repairbill repairbill){

        HashMap<String, Object> map = new HashMap<>();
        map.put("userNumber",repairbill.getUserNumber());
        map.put("repairType",repairbill.getRepairType());
        map.put("building",repairbill.getBuilding());
        map.put("repID",repairbill.getRepID());
        map.put("repairState",repairbill.getRepairState());

        Page<Repairbill> page = new Page<>(pageNum,pageSize);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.allEq(map,false);
        iRepairbillService.page(page, wrapper);
        return Result.success(page);
    }
    //评论
    @PutMapping("/evaluate")
    public Result evaluate(@RequestBody Repairbill repairbill){
        repairbill.setRepairState(5);
        System.out.println("evaluate==="+repairbill);
        iRepairbillService.updateById(repairbill);

        return Result.success("评论成功");
    }

    //分配维修员
    @PutMapping("/allocation")
    public Result allocation(@RequestBody Repairbill repairbill){
        System.out.println("repairbill=="+repairbill);
        repairbill.setRepairState(2);
        boolean update = iRepairbillService.updateById(repairbill);
        if (!update){
            return Result.failure("分配失败");
        }
        return Result.success("分配成功");
    }

    @GetMapping("/getErrorBillDetailById/{id}")
    public Result getErrorBillDetailById(@PathVariable(name = "id") Integer id){
        QueryWrapper<Repairbill> wrapper = new QueryWrapper<>();
        wrapper.eq("mainId",id);
        wrapper.select("errorReason","errorSuggest","errorDetail");
        Repairbill repairbill = iRepairbillService.getOne(wrapper);
        System.out.println(repairbill);
        if (repairbill==null){
            return Result.failure("获取失败");
        }

        return Result.success("异常信息获取成功",repairbill);
    }

    @GetMapping("/getRepairBillCount")
    public Result getRepairBillCount(){
        int count = iRepairbillService.count();
        return Result.success("获取成功",count);
    }

    @GetMapping("/getCountAndType")
    public Result getCountAndType(){
        Map<String, Object> map = iRepairbillService.selectAllCountFromType();
        return Result.success("获取成功",map);
    }

    @GetMapping("/getAllCountAndState")
    public Result getAllCountAndState(){
        Map<String, Object> map = iRepairbillService.selectAllCountAndState();
        return Result.success("获取成功",map);
    }


    @GetMapping("/getOneYearCountChange")
    public Result getOneYearCountChange(){
        Map<String, Object> map = iRepairbillService.selectOneYearCountChange();
        return Result.success("获取成功",map);
    }

    @GetMapping("/getOneYearSouthAndNotrhCountChange")
    public Result getOneYearSouthAndNotrhCountChange(){
        Map<String, Object> map = iRepairbillService.selectOneYearSouthAndNotrhCountChange();
        return Result.success("获取成功",map);
    }

    @GetMapping("/selectCountFromGroupBuilding")
    public Result selectCountFromGroupBuilding(){
        List<GroupBuildingVo> list = iRepairbillService.selectCountFromGroupBuilding();
        return Result.success("获取成功",list);
    }

    @GetMapping("getCountFromGroupGrade")
    public Result getCountFromGroupGrade(){
        List<Counts> list = this.iRepairbillService.selectCountFromGroupGrade();
        return Result.success("获取成功",list);
    }

    //撤销报修
    @DeleteMapping("/deleteOrderReport/{id}")
    public Result deleteOrderReport(@PathVariable(name = "id") Integer id){
        System.out.println(id);
        boolean b = iRepairbillService.removeById(id);
        if (!b) {
            return Result.failure("撤销失败");
        }
        return Result.success("撤销成功");
    }
}
